package org.bigjava.user.service.impl;

import org.bigjava.bean.Types;
import org.bigjava.user.mapper.TypesMapper;
import org.bigjava.user.service.TypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TypesServiceImpl implements TypesService {
    @Autowired
    TypesMapper typesMapper;

    //实现查询所有一级分类
    @Override
    public List<String> queryAllPrimaryClassification() {
        return typesMapper.queryAllPrimaryClassification();
    }


    //实现修改分类信息(管理员用）
    @Override
    public void updatePrimaryClassification(Types type) {
        typesMapper.updatePrimaryClassification(type);
    }


    //实现添加一级分类
    @Override
    public void insertPrimaryClassification(String typename) {
        typesMapper.insertPrimaryClassification(typename);
    }


    //实现添加二级分类（fid为一级分类的id）
    @Override
    public void insertSecondaryClassification(String onetypename, String twotypename) {
        Types type = typesMapper.queryTypeByName(onetypename);
        Map<String, Object> map = new HashMap<>();
        map.put("typename", twotypename);
        map.put("fid", type.getTypeId());
        typesMapper.insertSecondaryClassification(map);
    }


    //查询分类(支持模糊查询,和条件查询）
    @Override
    public List<Types> queryType(Types type) {
        return typesMapper.queryType(type);
    }


    @Override
    //实现查询一级分类的二级分类
    public List<String> querySecondaryClassificationByone(String typename) {
        Types types = typesMapper.queryTypeByName(typename);
        return typesMapper.querySecondaryClassificationByone(types.getTypeId());
    }


    //实现删除分类(如果是删除一级分类会删除一级分类下的所有二级分类)
    @Override
    public void delteType(String typename) {
        Types types = typesMapper.queryTypeByName(typename);
        Map<String, Object> map = new HashMap<>();
        if (types.getTypeFid() == 0) {
            map.put("typename", typename);
            map.put("Fid", types.getTypeId());
        } else {
            map.put("typename", typename);
            map.put("Fid", null);
        }
        typesMapper.delteType(map);
    }


    //根据名字查询分类
    @Override
    public Types queryTypeByName(String typename) {
        return typesMapper.queryTypeByName(typename);
    }


}

